package com.billsystem.service;

import com.billsystem.mapper.CategoryMapper;
import com.billsystem.mapper.RecordMapper;
import com.billsystem.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private RecordService recordService;
    /*
    增加消费收入类型
     */
    public void addCategory(Category category){
        categoryMapper.addCategory(category);
    }
    /*
    删除某种种类
     */
    @Transactional
    public void deleteCategory(Category category){
        List<Integer> list = recordMapper.queryRecordByCategory(category.getId());
        if (list != null) {
            for (int id : list) {
                recordService.deleteRecord(id);
            }
        }
        categoryMapper.deleteCategory(category);
    }

    /*
    根据id查询消费类型
     */
    public Category queryCategory(int id){
        return categoryMapper.queryCategory(id);
    }
    /*
    根据用户id，以及消费还是支出查询消费类型或者收入类型
     */
    public List<Category> queryListByUid(int uid, char incomeExpend){
        return categoryMapper.queryListByUid(uid, incomeExpend);
    }
    /*
    更新消费收入种类
     */
    public void updateCategory(Category category){
        categoryMapper.updateCategory(category);
    }

}
